package com.tjetc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tjetc.vo.Result;
import com.tjetc.constant.ShiMingCheckEnum;
import com.tjetc.domain.User;
import com.tjetc.mapper.UserMapper;
import com.tjetc.util.IdCardCheckUtil;
import com.tjetc.vo.Result;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.tjetc.domain.Patient;
import com.tjetc.mapper.PatientMapper;
import com.tjetc.service.PatientService;

import java.util.List;

@Service
public class PatientServiceImpl implements PatientService{
    @Resource
    private UserMapper userMapper;
    @Resource
    private PatientMapper patientMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return patientMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Patient record) {
        return patientMapper.insert(record);
    }

    @Override
    public int insertSelective(Patient record) {
        return patientMapper.insertSelective(record);
    }

    @Override
    public Patient selectByPrimaryKey(Integer id) {
        return patientMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Patient record) {
        return patientMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Patient record) {
        return patientMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Patient> selectByUid(Integer uid) {
     return    patientMapper.selectByUidBind(uid);
    }

    @Override
    public Result insertPatient(Patient patient) {
//        ShiMingCheckEnum check = IdCardCheckUtil.check(patient.getName(), patient.getIdcard());
//        if (check==ShiMingCheckEnum.ERROR){
//            return Result.fail("添加失败");
//        }else if (check==ShiMingCheckEnum.NOT_EXIST){
//            return Result.fail("身份证信息不存在,请输入正确的身份证");
//        }else if (check==ShiMingCheckEnum.INCONSISTENT){
//            return Result.fail("名字和身份证不匹配,请正确输入");
//        }

        Patient patient1= patientMapper.selectByIdcard(patient.getIdcard());
        if(patient1!=null){
            Integer uid = patient1.getUid();
            List<Patient> patients = patientMapper.selectByUid(uid);
            List<Patient> patients1 = patientMapper.selectByUidBind(uid);
            if (patients.size()>=8){
                return Result.fail("该用户已经绑定了8个患者");
            }else if (patients1.size()>=5){
                return Result.fail("该用户已经有5个常用就诊人");
            }
        }
        if (patient1!=null&&patient1.getIsbind()==0){
            patient.setId(patient1.getId());
            int i = patientMapper.updateByPrimaryKeySelective(patient);
            return i==1?Result.ok():Result.fail("添加失败");
        }else if (patient1==null) {
            int insert = patientMapper.insert(patient);
            return Result.ok();
        }else {
            User user = userMapper.selectByPrimaryKey(patient1.getUid());
            String name = user.getName();

            String substring = name.substring(name.length() - 1);
            String xing="";
            for (int i = 1; i < name.length(); i++) {
                xing+="*";
            }

            return Result.fail("用户已经被"+(xing+substring)+"绑定了");
        }
    }

    @Override
    public Result upbind(Integer id) {
        Patient patient = patientMapper.selectByPrimaryKey(id);
        patient.setIsbind(0);
        int i = patientMapper.updateByPrimaryKey(patient);
        return i==1?Result.ok():Result.fail("解绑失败");
    }

    @Override
    public List<Patient> selectByUidUnBind(Integer uid) {
        return patientMapper.selectByUidNoBind(uid);
    }

    @Override
    public Result updatePatient(Patient patient) {
        int i = patientMapper.updateByPrimaryKeySelective(patient);
        return i==1?Result.ok():Result.fail("修改失败");
    }

    @Override
    public Result checkIdCard(Integer id, String input) {
        Patient patient = patientMapper.selectByPrimaryKey(id);
        String idcard = patient.getIdcard();
        if (idcard.substring(idcard.length() - 6).equals(input)){
            return Result.ok();
        }
        return Result.fail("身份信息不匹配");
    }

    @Override
    public PageInfo<Patient> findAllPatient(Integer curPage,Integer pageSize,String name) {
        PageHelper.startPage(curPage,pageSize);
        List<Patient> list = patientMapper.findAllPatient(name);
        return new PageInfo<>(list);
    }


}
